package com.lianniu.web.tea.user.comment.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.tea.user.comment.model.TeaComment;
import com.lianniu.web.tea.user.comment.service.ITeaUserCommentService;
import com.lianniu.web.util.DateUtil;
import com.lianniu.web.util.TeaContant;
import com.lianniu.web.util.UUIDUtil;

@Controller
@RequestMapping("/rest/comment")
public class TeaRestUserCommentController {

	private final static Logger logger = Logger.getLogger(TeaRestUserCommentController.class);

	@Autowired
	private ITeaUserCommentService teaUserCommentService;
	
	@RequestMapping("/saveComment")
	public @ResponseBody BaseResult saveComment(HttpServletRequest req){
		logger.info("TeaRestUserCommentController  saveComment begin.");
		try {
			logger.info("saveComment begin.");
			String id = req.getParameter("id");
			String userId = req.getParameter("userId");
			String msg = req.getParameter("msg");
			String imgs = req.getParameter("imgs");
			TeaComment teaComment = new TeaComment();
			teaComment.setId(UUIDUtil.getUUID());
			teaComment.setOrderid(id);
			teaComment.setUserid(Integer.valueOf(userId));
			teaComment.setMark(0);
			teaComment.setMsg(msg);
			teaComment.setAddtime(DateUtil.getNowTime());
			teaComment.setPics(imgs);
			BaseResult baseResult = teaUserCommentService.saveComment(teaComment);
			logger.info("saveComment end.");
			logger.info("TeaRestUserCommentController saveComment end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaRestUserCommentController saveComment error:" + e.getMessage());
			logger.error("TeaRestUserCommentController saveComment end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	
	@RequestMapping("/saveZiXunComment")
	public @ResponseBody BaseResult saveZiXunComment(HttpServletRequest req){
		logger.info("TeaRestUserCommentController  saveZiXunComment begin.");
		try {
			logger.info("saveZiXunComment begin.");
			String id = req.getParameter("id");
			String userId = req.getParameter("userId");
			String msg = req.getParameter("msg");
			String imgs = req.getParameter("imgs");
			TeaComment teaComment = new TeaComment();
			teaComment.setId(UUIDUtil.getUUID());
			teaComment.setOrderid(id);
			teaComment.setUserid(Integer.valueOf(userId));
			teaComment.setMark(0);
			teaComment.setMsg(msg);
			teaComment.setAddtime(DateUtil.getNowTime());
			teaComment.setPics(imgs);
			BaseResult baseResult = teaUserCommentService.saveComment(teaComment);
			logger.info("saveZiXunComment end.");
			logger.info("TeaRestUserCommentController saveZiXunComment end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaRestUserCommentController saveZiXunComment error:" + e.getMessage());
			logger.error("TeaRestUserCommentController saveZiXunComment end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping("/getCommentList")
	public @ResponseBody BaseResult getCommentList(HttpServletRequest req){
		logger.info("TeaRestUserCommentController  getCommentList begin.");
		BaseResult baseResult = null;
		try {
			logger.info("getCommentList begin.");
			String type = req.getParameter("type");
			String orderId = req.getParameter("orderId");
			String userId = req.getParameter("userId");
			if(TeaContant.COMMENT_LIST_TYPE_ALL.equals(type)){
				//查询全部
				baseResult = teaUserCommentService.getCommentListALl(orderId,userId);
			}else if(TeaContant.COMMENT_LIST_TYPE_PART.equals(type)){
				baseResult = teaUserCommentService.getCommentListPart(orderId);
			}
			logger.info("getCommentList end.");
			logger.info("TeaRestUserCommentController getCommentList end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaRestUserCommentController getCommentList error:" + e.getMessage());
			logger.error("TeaRestUserCommentController getCommentList end.");
			return BaseResult.exception(e.getMessage());
		}
	}
}
